/* Edge Using Custom Render to display different arrow's head and several line style*/
package renders.edge
{
	import flash.display.Graphics;
	
	import org.un.cava.birdeye.ravis.enhancedGraphLayout.visual.edgeRenderers.EdgeDrawer;
	import org.un.cava.birdeye.ravis.graphLayout.visual.IVisualEdge;
	
	import renders.edge.arrows.DashedBody;
	import renders.edge.arrows.DefaultBody;
	import renders.edge.arrows.DefaultHead;
	import renders.edge.arrows.DependancyHead;
	import renders.edge.arrows.IArrowBodyDrawer;
	import renders.edge.arrows.IArrowHeadDrawer;
	import renders.edge.arrows.InheritanceHead;
	

	
	public class ExtendedEdge  extends EdgeDrawer 
	{

		private var headDrawer:IArrowHeadDrawer;
		private var bodyDrawer:IArrowBodyDrawer;
		private var _data:Object;
		
		private var _alpha:Number;
		private var _color:uint;
		
		public const DEPENDENCY_HEAD:String = "dependancy";
		public const INHERITANCE_HEAD:String = "inheritance";
		public const DEFAULT_HEAD:String = "default";
		
		public const DASHED_LINE:String = "dashed";
		public const SIMPLE_LINE:String = "simple";
		
		
		public function ExtendedEdge(g:Graphics):void {
			super(g);
			this.headDrawer = new DefaultHead;
			this.bodyDrawer = new DefaultBody;
			arrowLength +=7;
		}
		public function set arrowHeadShape(h:IArrowHeadDrawer):void
		{
			this.headDrawer = h;
		}
		public function set arrowBodyShape(b:IArrowBodyDrawer):void
		{
			this.bodyDrawer = b;
		}
		
		public function line(g:Graphics, ptx:Number, pty:Number):void{
			super.lineTo(g,ptx,pty);
		}
		public function move(g:Graphics, ptx:Number, pty:Number):void{
			super.moveTo(g,ptx,pty);
		}
		
		public override function draw(vedge:IVisualEdge):void {
			
			var head:String 	= vedge.data.head;
			var body:String 	= vedge.data.line;
			var color:String 	= vedge.data.color;
			var alpha:String 	= vedge.data.alpha;
			var toID:String 	= vedge.data.toID;
			var fromID:String 	= vedge.data.fromID;
			
			if(head == null)
				head=" ";
			if(body == null)
				body=" ";
			if(color == null)
				color="0x000000";
			if(alpha == null)
				alpha="1";
			
			if (toID.localeCompare("0")==0 || fromID.localeCompare("0")==0)
				{
				vedge.isVisible = false;
				super.draw(vedge);
				return;
				}
				
				
			_color =  parseInt(color, 16);
			_alpha = Number(alpha);		
				
			
			if (head.localeCompare(DEPENDENCY_HEAD)==0)
			{
				this.headDrawer = new DependancyHead;
			}
			else if (head.localeCompare(INHERITANCE_HEAD)==0)
			{
				this.headDrawer = new InheritanceHead;
			}
			else if (head.localeCompare(DEFAULT_HEAD)==0)
			{
				this.headDrawer = new DefaultHead;
			}
			
			if (body.localeCompare(DASHED_LINE)==0)
			{
				this.bodyDrawer = new DashedBody;
			}
			else if (body.localeCompare(SIMPLE_LINE)==0)
			{
				this.bodyDrawer = new DefaultBody;
			}
			
			
			super.draw(vedge);
		}
		
		protected override function drawArrow(fromX:Number, fromY:Number, toX:Number, toY:Number):void{
     		if (enableArrow == false)
     			return;
     				
     		var eps:Number = 0.0000000000001;
     		if (fromY == toY)
     			fromY += eps;
     		
     		if (fromX == toX)
     			fromX += eps;
     			
     		var dXY:Number = (fromY - toY) / (fromX - toX);
     		     		
     		var arrowOS:Number;
	        if (fromX >= toX)
	        {
	            arrowOS = 155;
	        }
	        else
	        {
	            arrowOS = 25;
	        }	
	                
	        var arrowLine1:Object = this.calculatePoint(toX, toY, arrowLength, 180 - Math.atan(dXY) * 5.729578E+001 - arrowOS);
	        var arrowLine2:Object = this.calculatePoint(toX, toY, arrowLength, 180 - Math.atan(dXY) * 5.729578E+001 + arrowOS);   		
     		
     		
     		this.headDrawer.drawArrowsHead(_g,this,_color,_alpha ,toX,toY,arrowLine1,arrowLine2);
     	
		}
		
		
		protected override function drawLine(x1:Number, y1:Number, x2:Number, y2:Number):void
		{
			 
			bodyDrawer.drawLine(_g,this,5,_color,_alpha,x1,y1,x2,y2);
			
		}
		
	}
}